//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.Extensibility;

namespace OpenLS.Spreadsheet
{
    partial class Cell : ICell
    {
        #region ICell Members

        IWorksheet ICell.Worksheet
        {
            get { return Worksheet; }
        }

        IRange ICell.ToRange()
        {
            return new SingleCellRange(this);
        }

        object ICell.Value
        {
            get { return Value; }
        }

        bool ICell.ContainsFunction(string functionName)
        {
            Expression exp = Expression;
            if (exp == null)
                return false;
            foreach (Expression e in ExpressionHelper.GetAllExpressions(exp))
            {
                if (e is FunctionInvokeExpression)
                {
                    FunctionInvokeExpression fe = (FunctionInvokeExpression) e;
                    if (fe.Function.Name == functionName)
                        return true;
                }
            }
            return false;
        }


        object ICell.Info(string info_type)
        {
            Cell reference = this;
            switch (info_type)
            {
                case "address":
                    return reference.Label;
                case "col":
                    return reference.Column.Index + 1;
                case "color":
                    return reference.Background != null ? 1 : 0;
                case "contents":
                    return reference.Value;
                case "coord":
                    return reference.Label;
                case "filename":
                    return reference.Worksheet.Workbook.Document.FileName;
                case "format":
                    return ExternalFormat(reference.Format.Code);
                case "parentheses":
                    if (Format != null && Format.Code.Contains("("))
                        return 1;
                    return 0;
                case "prefix":
                    switch (HorizontalAlignment)
                    {
                        case HorizontalCellAlignment.Left:
                            return "'";
                        case HorizontalCellAlignment.Right:
                            return "\"";
                        case HorizontalCellAlignment.Center:
                            return "^";
                        case HorizontalCellAlignment.Fill:
                            return "\\";
                        default:
                            return "";
                    }
                case "protect":
                    return this.IsLocked;
                case "row":
                    return reference.Row.Index + 1;
                case "type":
                    {
                        if (Value == null)
                            return "b";
                        if (Value is string || Value is Complex)
                            return "l"
                                ;
                        return "v";
                    }
                case "width":
                    return (int) reference.Column.ActualCharacterWidth;
            }
            return Errors.ValueError;
        }

        private string ExternalFormat(string code)
        {   
            switch (code.ToLower())
            {
                case "general":
                    return "G";

                case "0"
                    :
                    return "F0";
                case "#,##0":
                    return ",0";
                case "0.00":
                    return "F2";
                case "#,##0.00":
                    return ",2";
                case "$#,##0_);($#,##0)":
                    return "C0";
                case "$#,##0_);[red]($#,##0)":
                    return "C0-";
                case "$#,##0.00_);($#,##0.00)":
                    return "C2";
                case "$#,##0.00_);[red]($#,##0.00)":
                    return "C2-";
                case "0%":
                    return "P0";
                case "0.00%":
                    return "P2";
                case "0.00e+00":
                    return "S2";
                case "# ?/? or # ??/??":
                    return "G";
                case "m/d/yy":
                case "m/d/yy h:mm":
                case "mm/dd/yy":
                case "m/d/yyyy":
                    return "D4";
                case "d-mmm-yy":
                case "dd-mmm-yy":
                    return "D1";
                case "d-mmm":
                case "dd-mmm":
                    return "D2";
                case "mmm-yy":
                    return "D3";
                case "mm/dd":
                    return "D5";
                case "h:mm am/pm":
                    return "D7";
                case "h:mm:ss am/pm":
                    return "D6";
                case "h:mm":
                    return "D9";
                case "h:mm:ss":
                    return "D8";
            }
            return "G";


        }


        int ICell.ColumnIndex
        {
            get { return Column.Index; }
        }

        int ICell.RowIndex
        {
            get { return Row.Index; }
        }

        #endregion

    }
}